package part_2;

import java.io.FileNotFoundException;
import java.io.IOException;

public class Process {
	
	public static void main(String[] args) {
		
		double[][] data = new double[6][6];
		
		//Get the data from the file
		try {		
			data = fileToArray("/marathons.txt"); 
		}
		catch (FileNotFoundException e1	) {
			System.out.println("A fatal error accourd trying to open the data file ...");
			e1.printStackTrace();
			System.exit(1);
		} catch (IOException e2) {
			System.out.println("A fatal error accourd with the IO");
			e2.printStackTrace();
			System.exit(1);
		}
		//display the data
		System.out.println("The raw data:");
		for(double[] marathon : data) {
			dumparray(marathon);
		}
		System.out.println();
		
		//get the average player time
		double[] averagePlayerTime = averagePlayerTime(data);
		System.out.println("The average player times are:");
		dumparray(averagePlayerTime);
		System.out.println();
		
		//get the average marathon time
		double[] averageMarathonTime = averageMarathonTime(data);
		System.out.println("The average marathon times are:");
		dumparray(averageMarathonTime);
		System.out.println();
		
		//TODO: read min / max
		
		//the min is:
		double[] tmpvar = findMin(data);
		System.out.println("A minimum was reached at row:"+((int)tmpvar[0]+1)+" and column:"+((int)tmpvar[1]+1)+" is:" + tmpvar[2]);
		
		//The max is:
		tmpvar = findMax(data);
		System.out.println("A maximum was reached at row:"+((int)tmpvar[0]+1)+" and column:"+((int)tmpvar[1]+1)+" is:" + tmpvar[2]);
		
		System.out.println();
		System.out.println("Done exiting ...");
	}
	
	//Calc average time/marathon
	private static double[] averageMarathonTime(double[][] data) {
		
		double[] averageTime = new double[data.length];
		
		for(int i = 0; i < data.length; i++) {
			double sum = 0;
			int validRuns = 0;
			for(int j = 0; j < data[0].length; j++) {
				if (data[j][i] != 0) {
					sum += data[j][i];
					validRuns++;
				}
			}
			averageTime[i] = sum / validRuns;
		}
		return averageTime;
		
	}

	//Calc average time/player
	private static double[] averagePlayerTime(double[][] data) {
		double[] averageTime = new double[data.length];
		for(int i = 0; i < data[0].length; i++) {
			double sum = 0;
			int validRuns = 0;
			for(int j = 0; j < data.length; j++) {
				if (data[i][j] != 0) {
					sum += data[i][j];
					validRuns++;
				}
			}
			averageTime[i] = sum / validRuns;
		}
		return averageTime;
	}

	//convert the data in a file to a 6x6 array
	private static double[][] fileToArray(String path) throws FileNotFoundException, IOException {
		double[][] data = new double[6][6];
		
		ReadFile reader = new ReadFile(path);
		
		for(int i = 0; i < data.length; i++) {
			for(int j = 0; j < data.length; j++) {
				data[i][j] = reader.readDouble();
			}
		}
		
		reader.close();
		return data;
	}

	//find the min of a 2 dim array
	private static double[] findMin(double[][] data) {
		double[] min = {0, 0, data[0][0]};
		
		for(int i = 0; i < data.length; i++) {
			for(int j = 0; j < data[0].length; j++) {
				if (data[i][j] < min[2] && data[i][j] != 0) {
					min[0] = i;
					min[1] = j;
					min[2] = data[i][j]; 
				}
			}
		}
		return min;
	}

	//find the max of a 2 dim array
	private static double[] findMax(double[][] data) {
		double[] max = {0, 0, data[0][0]};
		
		for(int i = 0; i < data.length; i++) {
			for(int j = 0; j < data[0].length; j++) {
				if (data[i][j] > max[2]) {
					max[0] = i;
					max[1] = j;
					max[2] = data[i][j]; 
				}
			}
		}
		return max;
	}
	
	//displays an array on  a single line
	private static void dumparray(double[] array) {
		for (int i=0;i < array.length; i++) {
			System.out.print(array[i]+"\t");
		}
		System.out.println();
	}
}
